<?php

namespace app\common\model;

use think\Model;


class Connection extends Model
{





    // 表名
    protected $name = 'connection';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'estimate_time_text',
        'status_text',
        'report_time_text',
        'strike_data_text',
        'strike_time_text',
        'lapse_time_text',
        'type_data_text',
        /*'collision_data_text',
        'collision_time_text'*/
    ];



    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4')];
    }

    public function getStrikeDataList()
    {
        return ['0' => __('Strike_data 0'), '1' => __('Strike_data 1')];
    }

    public function getCollisionDataList()
    {
        return ['0' => __('Collision_data 0'), '1' => __('Collision_data 1')];
    }
    public function getTypeDataList()
    {
        return ['0' => __('Type_data 0'), '1' => __('Type_data 1')];
    }

    public function getTypeDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type_data']) ? $data['type_data'] : '');
        $list = $this->getTypeDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getEstimateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['estimate_time']) ? $data['estimate_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReportTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['report_time']) ? $data['report_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStrikeDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['strike_data']) ? $data['strike_data'] : '');
        $list = $this->getStrikeDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStrikeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['strike_time']) ? $data['strike_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLapseTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['lapse_time']) ? $data['lapse_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCollisionDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['collision_data']) ? $data['collision_data'] : '');
        $list = $this->getCollisionDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCollisionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['collision_time']) ? $data['collision_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setEstimateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReportTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setStrikeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLapseTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCollisionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function staff()
    {
        return $this->belongsTo('Staff', 'staff_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function bindStaff()
    {
        return $this->belongsTo('Staff', 'bind_staff_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function canal()
    {
        return $this->belongsTo('Canal', 'canal_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 检测绑定是否过期
     * */
    public function checkExpire($connection_id=0)
    {
        $where="1=1";
        if(!empty($connection_id)) $where=["id"=>$connection_id];
        $time=" lapse_time>0 and UNIX_TIMESTAMP(FROM_UNIXTIME(lapse_time,'%Y-%m-%d'))  <= ".strtotime(date("Y-m-d",strtotime("-1 days",time())));
        if(!empty($connection_id)){
            $time=" lapse_time>0 and UNIX_TIMESTAMP(FROM_UNIXTIME(lapse_time,'%Y-%m-%d %H'))  <= ".strtotime(date("Y-m-d H",strtotime("-1 days",time())));
        }
        $list= $this->where($where)->where($time)
            ->where("status !='4' and status !='3' and bind_staff_id is not null")
            ->find();
        if(!empty($list)){
            /* $this->where("status !='4' and status !='3' and bind_staff_id is not null")
                 ->where($where)
                 ->where($time)
                 ->update(["status"=>"3","bind_staff_id"=>0]);*/
            $this->where("status ='2' and bind_staff_id is not null")
                ->where($where)
                ->where($time)
                ->update(["status"=>"3","bind_staff_id"=>0]);
            $this->where("(status ='0' or status ='1') and bind_staff_id is not null")
                ->where($where)
                ->where($time)
                ->update(["bind_staff_id"=>0]);
        }
        return true;
    }

    /**
     * 修改昨天报备客户为已录入
     * */
    public function reportExpire()
    {
        $where="1=1";
        $time=" UNIX_TIMESTAMP(FROM_UNIXTIME(report_time,'%Y-%m-%d'))  <= ".strtotime(date("Y-m-d",strtotime("-1 days",time())));
        $list= $this->where($where)->where($time)
            ->where(["status"=>'1'])
            ->find();
        if(!empty($list)){
            $this->where(["status"=>'1'])
                ->where($where)
                ->where($time)
                ->update(["status"=>"0","bind_staff_id"=>0]);
        }
        return true;
    }
}
